from dynamic import get_hsv_from_file
from press_image import calculate_average_color
import cv2
hsv_list = get_hsv_from_file('hsv_record.pkl')
color_list = []
img = cv2.imread("images/color_recognition/rect4.jpg")
num = 4
for index, hsv in enumerate(hsv_list):
    mask = cv2.inRange(img, hsv[0], hsv[1])
    mask = cv2.erode(mask, (5, 5), iterations=5)
    cv2.imshow('mask', mask)
    cv2.waitKey(0)
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    color_list.append(calculate_average_color(contours[0], img))
    if index == 4:
        x, y, w, h = cv2.boundingRect(contours[0])
        tx = int(w / 2 + x) - 20
        ty = int(h / 2 + y) + 20
        text = str(num)
        color = color_list[num - 1]
        # cv2.putText(img, text, (tx, ty), cv2.FONT_HERSHEY_SIMPLEX, 3, color, 3)
        # 图像，文字内容， 坐标 ，字体，大小，颜色，字体厚度
        cv2.putText(img, text, (tx, ty), cv2.FONT_HERSHEY_SIMPLEX, 2, color, 2)
        cv2.imshow('Original', img)

cv2.imshow('mask', mask)
#cv2.imshow('Original', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
